package com.springboot.springmvc.restful.crud.dao.impl;

import com.springboot.springmvc.restful.crud.bean.Employee;
import com.springboot.springmvc.restful.crud.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EmployeeDaoimpl implements EmployeeDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public Employee getEmpById(Long id) {

        String sql = "select * from employee where id = ?";
        Employee employee = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Employee.class), id);
        return employee;

    }

    @Override
    public void addEmp(Employee employee) {

        String sql = "insert into employee(name,age,email,gender,address,salary) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,employee.getName(),employee.getAge(),employee.getEmail(),employee.getGender(),employee.getAddress(),employee.getSalary());

    }

    @Override
    public void updateEmp(Employee employee) {

        String sql = "update employee set name = ?,age = ?,email = ?,gender = ?,address = ?,salary = ? where id = ?";
        jdbcTemplate.update(sql,employee.getName(),employee.getAge(),employee.getEmail(),employee.getGender(),employee.getAddress(),employee.getSalary(),employee.getId());

    }

    @Override
    public void deleteEmp(Long id) {

        String sql = "delete from employee where id = ?";
        jdbcTemplate.update(sql,id);

    }

    @Override
    public List<Employee> getList() {
        String sql = "select * from employee";
        List<Employee> employees = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));
        return employees;
    }


}
